/*
 * TNSExceptionFrame.java
 *
 * --- Last Update: 4/18/2010 3:01 PM ---
 *
 * Update Notes 4/18/2010 3:01 PM by Adrian Wijasa:
 * This class is made to work with Java 5.
 *
 * Created on March 7, 2007, 3:02 PM
 *
 * CSV Loader
 * Copyright 2007, 2009, 2010 Adrian Wijasa
 *
 * This file is part of CSV Loader.
 *
 * CSV Loader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CSV Loader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CSV Loader.  If not, see <http://www.gnu.org/licenses/>.
 */

package forms;

import config.ConfigException;

import javax.swing.*;

/**
 * Error Message Window to be shown when a ConfigException is caught
 *
 * --- Last Update: 7/17/2009 2:28 PM ---
 *
 * Update Notes 7/17/2009 2:28 PM:
 * In an OK button click event:
 * Put try {} around:
 * main.fileChooser.setEnabled( true );
 * main.fileChooser.requestFocus();
 *
 * and catch a NullPointerException, just in case TNSExceptionFrame is invoked through the Main window, not the
 * File Chooser.  If the TNSExceptionFrame is invoked by Main window, the focus should be back to Main Window
 * after OK button is clicked.
 *
 * Also if fileChooser is not Visible, transfer the focus back to CSV Loader Main window instead.
 * 
 * @author matianyuan
 */
public class TNSExceptionFrame extends javax.swing.JFrame {

    /** Creates new form TNSExceptionFrame */
    public TNSExceptionFrame( Main main , ConfigException ex) {
        this.main = main;
        this.ex = ex;

        initComponents();
        setLocation( main.getX() + 77, main.getY() + 141 );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        leftScrollPane = new javax.swing.JScrollPane();
        leftTextPane = new javax.swing.JTextPane();
        okButton = new javax.swing.JButton();
        rightScrollPane = new javax.swing.JScrollPane();
        rightTextPane = new javax.swing.JTextPane();
        exceptionMessage = new javax.swing.JLabel(ex.getMessage());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("TNS Upload Error");

        leftScrollPane.setBorder(null);

        leftTextPane.setBackground(new java.awt.Color(236, 233, 216));
        leftTextPane.setBorder(null);
        leftTextPane.setEditable(false);
        leftTextPane.setText("ERROR: One or more TNS Configuration(s) in the selected file does not follow the following format:\n\n<TNS Name> =\n\t(DESCRIPTION =\n\t\t(ADDRESS =\n           \t\t\t(PROTOCOL = TCP)\n\t\t\t(Host = <Hostname>)\n            \t\t\t(Port = <Port>)\n            \t\t)\n            \t\t(CONNECT_DATA =\n           \t\t\t(SID = <SID>)\n            \t\t)\n           \t)\n\nIn addition, only one line break is allowed between TNS Configurations.");
        leftScrollPane.setViewportView(leftTextPane);

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        rightScrollPane.setBorder(null);

        rightTextPane.setBackground(new java.awt.Color(236, 233, 216));
        rightTextPane.setBorder(null);
        rightTextPane.setEditable(false);
        rightTextPane.add(exceptionMessage);
        rightTextPane.setText("The following TNSNAMES.ORA syntaxes are not supported:\n- DESCRIPTION_LIST\n- ADDRESS_LIST\n- FAILOVER\n- LOAD_BALANCE\n- RECV_BUF_SIZE\n- SDU\n- SEND_BUF_SIZE\n- SOURCE_ROUTE\n- TYPE_OF_SERVICE\n- FAILOVER_MODE\n- GLOBAL_NAME\n- HS\n- INSTANCE_NAME\n- RDB_DATABASE\n- SERVER\n- SERVICE_NAME\n\nOnly TCP Protocol is supported.");
        rightScrollPane.setViewportView(rightTextPane);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 647, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(okButton)
                    .add(leftScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 332, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rightScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 297, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 355, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, leftScrollPane)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, rightScrollPane))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(okButton)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        try {
            if( main.fileChooser.isVisible() ) {
                main.fileChooser.setEnabled( true );
                main.fileChooser.requestFocus();
            } else {
                main.setEnabled( true );
                main.requestFocus();
            }
        } catch( NullPointerException ne ) {
            main.setEnabled( true );
            main.requestFocus();
        }
        dispose();
}//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane leftScrollPane;
    private javax.swing.JTextPane leftTextPane;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane rightScrollPane;
    private javax.swing.JTextPane rightTextPane;
    private JLabel exceptionMessage;
    // End of variables declaration//GEN-END:variables

    private Main main;
    private ConfigException ex;
}
